/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import {TemplateView} from "../general/TemplateView";
import {spinner} from "../common.js";
import {NodeView} from './NodeView.js'

export class LoginView extends TemplateView {
    render(t, vm) {
        const disabled = vm => vm.isBusy;

        if(!window.provider){
            return t.div({className: "PreSessionScreen"},'createMetaMaskProvider程序出错')
        }
        
        return t.div({className: "PreSessionScreen"},[
            t.if(vm => !vm.accounts,(t,vm) => t.div([
                t.div({className: "logo"}),
                t.h1({className: "h1"},[vm.i18n`Welcome to Seer`]),
                t.p({className: "p1"},vm.i18n`Make your contacts more valuable`),
                t.p({className: "p2"},vm.i18n`Step 1：connect wallet`),
                t.button({className:'div_flex_btn',onClick: () => {vm.metamask()}
                },[
                    t.div({className:"bg"},""), 
                    t.p({className:"btn_text"}, "MetaMask")
                ]),
            ])),
            t.if(vm => vm.accounts,(t,vm) => t.div({className:'PreSessionScreen_div'},[
                t.div({className: "flex_head"},[
                    // t.button({
                    //     className: "button-utility LoginView_back",
                    //     // onClick: () => vm.goBack(),
                    //     disabled
                    // }),
                    t.div(),
                    t.button({
                        className: "button-utility LoginView_more",
                        onClick: () => vm.setIsAdd(true),
                        disabled
                    }),
                ]),
                
                t.div({className: "logo_bg"},[
                    t.div({className: "logo"}),
                    t.h1([vm.i18n`Node List`]),
                ]),

                t.div(
                    {className: {
                        'addHomeserver form-row text': true,
                        'showAddHomeserver': vm =>  vm.isAdd
                    }},
                    [
                        t.div({className: "flex_head"},[
                            t.div(),
                            t.button({
                                className: "button-utility LoginView_more LoginView_cancel",
                                onClick: () => vm.setIsAdd(false),
                                disabled
                            }),
                        ]),
                        t.label({for: "homeserver"}, vm.i18n`Add Homeserver`),
                        t.input({
                            id: "homeserver",
                            type: "text",
                            placeholder: vm.i18n`Your matrix homeserver`,
                            disabled:false,
                            onInput: event => vm.setHomeserver(event.target.value || ''),
                            // onChange: () => vm.queryHomeserver(),
                        }),
                        t.p({className: {
                            LoginView_forwardInfo: true,
                            hidden: vm => !vm.resolvedHomeserver
                        }}, vm => vm.i18n`You will connect to ${vm.resolvedHomeserver}.`),
                        t.if(vm => vm.errorMessage, (t, vm) => t.p({className: "error"}, vm.i18n(vm.errorMessage))),
                        t.if(vm => vm.isFetchingLoginOptions, t => t.div({className: "LoginView_query-spinner padding14"}, [spinner(t), t.p("Fetching available login options...")])),
                        t.div({className: "button-row"}, [
                            t.button({
                                className:{
                                    "button-action primary":true,
                                    "noprimary":vm => !vm.resolvedHomeserver
                                } ,
                                onClick: () => vm.onAdd(),
                                disabled:vm => !vm.resolvedHomeserver
                            }, vm.i18n`Add`)
                        ]),
                    ]
                ),

                t.if(vm => vm.isFetchingLoginOptions, t => t.div({className: "LoginView_query-spinner"}, [spinner(t), t.p("Fetching available login options...")])),
                t.if(vm => vm.errorMessage, (t, vm) => t.p({className: "error"}, vm.i18n(vm.errorMessage))),
                t.if(vm => vm.isLoading, t => t.div({className:'loading_div'},[t.p("loading..."),spinner(t)])),
                // t.mapView(vm => vm.passwordLoginViewModel, vm => vm ? new PasswordLoginView(vm): null),
                t.div({className:'node_list'},[t.mapView(vm => vm.nodeViewModel, vm => vm ? new NodeView(vm): null)]),
                // t.mapView(vm => true,vm => new NodeView(vm)),
                // t.mapView(vm => vm.loadViewModel, loadViewModel => loadViewModel ? new SessionLoadStatusView(loadViewModel) : null),
            ]))
        ])
    }
}

